import sun.awt.geom.AreaOp;

// 使用虚拟头节点
public class DummyHeadLinkedList<E> {



    public class Node{
        public E e;

        public Node next;



        public Node(E e,Node next){
            this.e = e;
            this.next = next;
        }


        public Node(E e){
            this(e,null);
        }

        public Node(Node next){
            this(null,next);
        }

        public Node(){
            this(null,null);
        }

        @Override
        public String toString(){
            return e.toString();
        }
    }

    private Node dummyHead;

    private int size;
    public  DummyHeadLinkedList(){
        size = 0;
        dummyHead = new Node(null,null);
    }

    // 获取联表长度
    public int getSize(){
        return size;
    }

    // 判断联表是否为空
    public boolean isEmpty(){
        return size == 0;
    }

    // 向链表头添加元素
    public void addFirst(E e) throws Exception {
        this.add(e,0);
    }

    // 向联表之间添加元素
    public void add(E e , int index) throws Exception {
        checkSize(index);
        Node prev = dummyHead;
            for (int i = 0; i < index; i++) {
                prev = prev.next;
            }
//                Node node  = new Node(e);
//                node.next = prev.next;
//                prev.next  = node;


            prev.next = new Node(e,prev.next);
            size ++ ;
    }

    public void checkSize(int index) throws Exception {
        if (index < 0 || index > size){
            throw new Exception("index不合法");
        }
    }


    // 查
    public E get(int index){

        if (index<0 || index>size){
            throw new IllegalArgumentException("index异常");
        }

        Node cur = dummyHead;
        for (int i = 0; i < index; i++) {
            cur  = cur.next;
        }
        return cur.e;
    }

    // 查询第一个
    public E getFirst(){
        return get(0);
    }

    // 查询最后一个
    public E getLast(){
        return get(size - 1);
    }

    // 替换掉某个位置的元素
    public void set(E e ,int index){
        if (index < 0 || index >= size){
            throw new IllegalArgumentException("index异常");
        }
        Node cur = dummyHead;
        for (int i = 0; i < index; i++) {
            cur = cur.next;
        }
        cur.e = e;
    }

    // 查询链表中是否包含元素e
    public boolean contains(E e){
        Node cur = dummyHead.next;
        while (cur!= null){
            if (cur.equals(e)){
                return true;
            }
            cur = cur.next;
        }
        return false;
    }

    public E remove(int index){
        if (index > size || index < 0){
            throw new IllegalArgumentException("index不合法");
        }
        Node cur = dummyHead.next;
        for (int i = 0; i < index-1; i++) {
            cur = cur.next;
        }
        Node retNode = cur.next;
        cur.next = retNode.next;
        retNode.next = null;
        size --;
        return (E) retNode;
    }

    public E removeFirst(){
        return remove(0);
    }

    public Node removeLast(){
        return (Node) remove(size -1 );
    }

    @Override
    public String toString(){
        Node cur = dummyHead.next;
        StringBuilder builder = new StringBuilder();
        while (cur != null){
            builder.append(cur + "->");
            cur = cur.next;
        }
        builder.append("null");
        return builder.toString();
    }


}
